﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using DunSilverlight.Comm;
using DunLibrary.Dun;

namespace DunSilverlight.Views
{
    public partial class ReceiveTypeOfCallPage : Page
    {
        public ReceiveTypeOfCallPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(ReceiveTypeOfCall_Loaded);
        }

        void ReceiveTypeOfCall_Loaded(object sender, RoutedEventArgs e)
        {
            Query();//加载所有接听类型数据
        }
        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            Query();
        }

        private void Query()
        {
            //在web定义一个wcf服务，这个服务继承BaseService,然后在前台添加一个服务(如果还没有添加的话)
            ReceiveTypeOfCallService.ReceiveTypeOfCallServiceClient ser = new ReceiveTypeOfCallService.ReceiveTypeOfCallServiceClient();

            //wcf所有函数都是异步执行，所以定义并添加一个回调函数
            ser.GetListByHQLCompleted += (object sender, ReceiveTypeOfCallService.GetListByHQLCompletedEventArgs e) => 
            {
                try
                {
                    int totalCount = 0;
                    //反序列化一个ReciveType的集合
                    IList<DunLibrary.Dun.ReceiveTypeOfCall> receiveTypes = JsonSerializerHelper.JsonToEntities<DunLibrary.Dun.ReceiveTypeOfCall>(e.Result, out totalCount);

                    dgeReciveType.ItemsSource = receiveTypes;
                    
                }
                catch (Exception ex)
                {
                    Tools.ShowMessage(ex.Message, "", false);
                }
            };
            //开始异步调用后台wcf服务,传入hql()
            ser.GetListByHQLAsync("from ReceiveTypeOfCall");
        }
        /// <summary>
        /// 新增一个接听类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            ReceiveTypeOfCall cmp = new ReceiveTypeOfCall();

            ReceiveTypeOfCallForm frmReciveType = new ReceiveTypeOfCallForm(cmp);

            Tools.OpenWindow("接听类型-新增", frmReciveType, null);
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            ReceiveTypeOfCall cmp = dgeReciveType.SelectedItem as ReceiveTypeOfCall;

            if (cmp != null)
            {
                ReceiveTypeOfCallForm frmReciveType = new ReceiveTypeOfCallForm(cmp);
                Tools.OpenWindow("接听类型-编辑", frmReciveType, null);
            }
        }
        /// <summary>
        /// 删除一个接听类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            ReceiveTypeOfCall cmp = dgeReciveType.SelectedItem as ReceiveTypeOfCall;

            if (cmp != null)
            {

                MessageBoxResult mr = MessageBox.Show(string.Format("是否要删除{0}", cmp.Name), "提示", MessageBoxButton.OKCancel);

                if (mr == MessageBoxResult.OK)
                {
                    ReceiveTypeOfCallService.ReceiveTypeOfCallServiceClient ser = new ReceiveTypeOfCallService.ReceiveTypeOfCallServiceClient();

                    ser.DeleteByIdCompleted += (object sender1, ReceiveTypeOfCallService.DeleteByIdCompletedEventArgs e1) =>
                    {
                        if (JsonSerializerHelper.JsonToEntity<bool>(e1.Result))
                        {
                            Tools.ShowMessage("删除成功!", "", true);
                        }
                    };

                    ser.DeleteByIdAsync(cmp.Id);
                }
            }
        }
        // 当用户导航到此页面时执行。
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

    }
}
